package com.fg.cloud.factory.controller.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.ArrayUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.product.ProductUnitEntity;
import com.fg.cloud.manager.service.product.ProductUnitService;

/**
 * Description:商品多单位
 * @author around
 * @date 2017年8月18日下午1:47:20
 */
@Controller
@RequestMapping(value="/unit")
public class ProductUnitController extends BaseController{

	@Autowired
	private ProductUnitService unitService ; 
	
	/**
	 * @Description: 新增单位信息 
	 * @param req
	 * @param resp
	 * @param unitEntity 单位信息实体
	 * @author liushengchao
	 * @date 2017年6月21日下午12:43:48
	 * @throws 
	 *
	 */
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req , HttpServletResponse resp , ProductUnitEntity unitEntity){
		try {
			unitEntity.setCreator(getCurrentFactoryUser().getId());
			unitEntity.setUpdater(getCurrentFactoryUser().getId());
			unitEntity.setFacId(getCurrentFactoryUser().getFacId());
			if(unitEntity ==null || StringUtils.isEmpty(unitEntity.getUnitName())){
				doResult(resp, false, StatusCode.LACKVALUE, null, "单位名不能为空");
			}else{
				
				// 单位名称重复性校验
				int num = unitService.findnum(unitEntity);
				if(num > 0){
					doResult(resp, false, StatusCode.SAVEERROR, null, "单位名称已存在");
					return;
				}
				int flag = unitService.insert(unitEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.ADD_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 修改单位信息 
	 * @param req
	 * @param resp
	 * @param unitEntity 单位信息实体
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月21日下午1:29:46
	 * @throws 
	 *
	 */
	@RequestMapping(value="/modify",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void update(HttpServletRequest req , HttpServletResponse resp , ProductUnitEntity unitEntity ){
		try {
			unitEntity.setFacId(getCurrentFactoryUser().getFacId());
			if(unitEntity == null || unitEntity.getId() == null || unitEntity.getId() < 0){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
			}else{
				unitEntity.setFacId(getCurrentFactoryUser().getFacId());
				unitEntity.setUpdater(getCurrentFactoryUser().getId());
				
				// 单位名称重复性校验
				int num = unitService.findnum(unitEntity);
				if(num > 0){
					doResult(resp, false, StatusCode.SAVEERROR, null, "单位名称已存在");
					return;
				}
				int flag = unitService.update(unitEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 根据ID查询单位信息 
	 * @param req
	 * @param resp 
	 * @param id 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月21日下午1:37:23
	 * @throws 
	 *
	 */
	@RequestMapping(value="/select",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void find(HttpServletRequest req , HttpServletResponse resp , @Param("id")Long id){
		try {
			if(id ==null || id<0){
				doResult(resp, true, StatusCode.FINDERROR, null, "id不能为空");
			}
			ProductUnitEntity unitEntity = unitService.find(id);
			if(unitEntity == null ){
				doResult(resp, true, StatusCode.FINDSUCCESS, unitEntity, ResultType.FIND_NULL);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, unitEntity, ResultType.FIND_SUCCESS);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 条件查询 
	 * @param req
	 * @param resp
	 * @param facId 厂商ID
	 * @param unitName  单位名称
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月21日下午2:14:56
	 * @throws 
	 *
	 */
	@RequestMapping(value="/selectpage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req , HttpServletResponse resp , 
			Long facId , String unitName,Integer page, Integer rows, String sort, String order){
		if (page == null || page < 0 || rows == null || rows < 0 || StringUtils.isEmpty(sort)
				|| StringUtils.isEmpty(order)) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
			return;
		}
		PageUtils<ProductUnitEntity> pageinfo = new PageUtils<ProductUnitEntity>(page, rows, sort, order);
		Map<String, Object> condition = new HashMap<String, Object>();

		facId = getCurrentFactoryUser().getFacId();
		if (facId != null && facId >= 0) {
			condition.put("facId", facId);
		}
		if (unitName != null && !"".equals(unitName)) {
			condition.put("unitName", unitName);
		}
		pageinfo.setCondition(condition);
		try {
			unitService.selectforPage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询全部单位
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月17日上午10:04:38
	 */
	@RequestMapping(value="/findAll",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void findAll(HttpServletRequest req , HttpServletResponse resp){
		try {
			Long facId = getCurrentFactoryUser().getFacId();
			List<ProductUnitEntity> list = unitService.findAll(facId);
			if (list.size() == 0) {
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_NULL);
			} else {
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	/**
	 * @Description: 删除商品单位 
	 * @param req
	 * @param resp
	 * @param id 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月21日下午2:17:04
	 * @throws 
	 */
	@RequestMapping(value="/delete",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void delete(HttpServletRequest req , HttpServletResponse resp , String ids){
		try {
			Long[] id;
			if(ids ==null || "".equals(ids)){
				id = new Long[0];
			}else{
				String[] id1 = ids.split(",");
				id = ArrayUtils.convert(id1);
			}
			Long faceId = getCurrentFactoryUser().getFacId();

			Map<String,Object> condition = new HashMap<String,Object>();
			condition.put("ids", id);
			condition.put("facId", faceId);
			int flag = 0;
			if(id.length>0){
				flag = unitService.deletes(condition);
			}else{
				flag = 1;
			}
			if(flag > 0 ){
				doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			}else{
				doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
}
